package com.rech.utils;

import android.util.Log;

import org.apache.commons.codec.binary.Hex;
import org.json.JSONObject;

import static android.content.ContentValues.TAG;

public class CommandUtil {



        private static final char[] HEXES = {
                '0', '1', '2', '3',
                '4', '5', '6', '7',
                '8', '9', 'a', 'b',
                'c', 'd', 'e', 'f'
        };

        /**
         * byte数组 转换成 16进制小写字符串
         */
        public static String bytes2Hex(byte[] bytes) {
            if (bytes == null || bytes.length == 0) {
                return null;
            }

            StringBuilder hex = new StringBuilder();

            for (byte b : bytes) {
                hex.append(HEXES[(b >> 4) & 0x0F]);
                hex.append(HEXES[b & 0x0F]);
            }

            return hex.toString();
        }

        /**
         * 16进制字符串 转换为对应的 byte数组
         */
        public static byte[] hex2Bytes(String hex) {
            if (hex == null || hex.length() == 0) {
                return null;
            }

            char[] hexChars = hex.toCharArray();
            byte[] bytes = new byte[hexChars.length / 2];   // 如果 hex 中的字符不是偶数个, 则忽略最后一个

            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
            }

            return bytes;
        }


    public static byte computeXor(byte[] inData, int len) {
        byte xor = 0;
        for (int i = 0; i < len; i++) {
            xor ^= inData[i];
        }
        return xor;
    }
    public static String toGetCommand(int io,int _485){
        String get = "9003XXYY1aa0";
        String cmd2 = "574b4c5909YY82XXZZ";
        String cmd3 = "574b4c5909YY82XX";
        if(_485 > 4)
        {
            String s_io = String.format("%02x", io);
            String s_485 = String.format("%02x", _485);


            cmd3 = cmd3.replaceFirst("XX",s_io);
            cmd3 = cmd3.replaceFirst("YY",s_485);
            cmd2 = cmd2.replaceFirst("XX",s_io);
            cmd2 = cmd2.replaceFirst("YY",s_485);
            byte[] bytes = new byte[8];
            try{
                bytes =  Hex.decodeHex(cmd3.toCharArray());
            }catch (Exception e){
                Log.d(TAG, "toGetCommand: ");
            }
            byte xor = 0;
            for (int i = 0; i < 8; i++) {
                xor ^= bytes[i];
            }
            String s_xor = String.format("%02x",xor);
            cmd2 = cmd2.replaceFirst("ZZ",s_xor);
            return cmd2;
        }else{
            String s_io = String.format("%02x", io);
            String s_485 = String.format("%02x", _485);
            if (s_io.equals("00")) {
                get = get.replaceFirst("XX",s_io);
                get = get.replaceFirst("YY",s_485);
            } else {
                get = get.replaceFirst("YY",s_io);
                get = get.replaceFirst("XX",s_485);
            }

            return get;
        }

    }
    public static byte[] toGetByteCommand(String arg){
        byte[] bytes = new byte[12];
        try{
            bytes =  Hex.decodeHex(arg.toCharArray());
        }catch (Exception e){
            Log.d(TAG, "toGetCommand: ");
        }
        return bytes;

    }
    public static byte[] toGetByteCommand2(String arg){
        byte[] bytes = new byte[18];
        try{
            bytes =  Hex.decodeHex(arg.toCharArray());
        }catch (Exception e){
            Log.d(TAG, "toGetCommand: ");
        }
        return bytes;

    }

    public static JSONObject toNotifyCommand(String strCmd){
        JSONObject obj= new JSONObject();
        try{

            if(strCmd.indexOf("1a a0") > 0){
                obj.put("code",100);
            }
            else if(strCmd.indexOf("3a a0") > 0){
                obj.put("code",100);
            }
            else if(strCmd.indexOf("4a a0") > 0){
                obj.put("code",200);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return obj;
    }
}
